﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

using $safeprojectname$.Service;

namespace $safeprojectname$.Storage
{
    public static class SessionManager
    {
        public static ISessionStorage GetSessionStorage(HttpContextBase context)
        {
            return Current.GetSessionStorage(context);
        }
        public static ISessionStorage GetSessionStorage(HttpContext context)
        {
            return Current.GetSessionStorage(context);
        }
        public static ISessionStorage GetSessionStorage()
        {
            return Current.GetSessionStorage();
        }
        public static void Shutdown()
        {
            Instance.Shutdown();
        }

        #region Provider
        static ISessionStorageProvider Current
        {
            get { return Instance.Provider; }
        }
        #endregion Provider

        #region lazy instance
        /// <summary> .NET lazy loader </summary>
        static class Instance
        {
            // beforefieldinit off
            // see: http://www.yoda.arachsys.com/csharp/singleton.html
            // see: http://www.yoda.arachsys.com/csharp/beforefieldinit.html

            #region members
            static ISessionStorageProvider _provider;
            static readonly object Locker = new object();
            // constructor
            static Instance() { } // lazy loader
            #endregion members

            #region public
            public static ISessionStorageProvider Provider
            {
                get
                {
                    lock (Locker)
                    {
                        if (_provider.IsNull())
                        {
                            _provider = Factory.CreateInstance<ISessionStorageProvider>();
                        }
                        return _provider;
                    }
                }
            }
            public static void Shutdown()
            {
                lock (Locker)
                {
                    if (_provider.IsNull())
                    {
                        return;
                    }
                    _provider.Shutdown();
                    _provider = null;
                }
            }
            #endregion public
        }
        #endregion lazy instance
    }
}
